package com.example.qrcode.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.qrcode.entity.Product;
import com.example.qrcode.service.ProductService;
import com.example.qrcode.vo.ProductVO;
import com.example.qrcode.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 分页查询产品
     * @param pageNum 页码，默认1
     * @param pageSize 每页条数，默认10
     */
    @GetMapping("/page")
    public ResultVo<IPage<Product>> getProductPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        
        IPage<Product> productPage = productService.getProductPage(pageNum, pageSize);
        return ResultVo.success(productPage);
    }


    @GetMapping("/pageWithDetail")
    public ResultVo<IPage<ProductVO>> getProductsWithDetail(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        System.out.println("getProductsWithDetail");
        System.out.println("pageNum: " + pageNum);
        System.out.println("pageSize: " + pageSize);
        IPage<ProductVO> productPage = productService.getProductsWithDetail(pageNum, pageSize);
        
        // 调试：检查返回的数据
        if (productPage != null && productPage.getRecords() != null) {
            System.out.println("=== 返回的产品数据调试 ===");
            System.out.println("总记录数: " + productPage.getTotal());
            System.out.println("当前页记录数: " + productPage.getRecords().size());
            
            for (int i = 0; i < productPage.getRecords().size(); i++) {
                ProductVO item = productPage.getRecords().get(i);
                System.out.println(String.format("产品%d: productSNo=%s, lineNum=%s, lineNumType=%s, deviceId=%s", 
                    i + 1, 
                    item.getProductSNo(), 
                    item.getLineNum(),
                    item.getLineNum() != null ? item.getLineNum().getClass().getName() : "null",
                    item.getDeviceId()
                ));
            }
        }
        
        return ResultVo.success(productPage);
    }


    @GetMapping("/test")
    public ResultVo<String> test() {
        return ResultVo.success("测试接口  product");
    }
}
